package malitech.yadam.model.acquire;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import malitech.framework.model.BaseEntity;
import malitech.framework.util.DefaultFeedBackHandler;
import malitech.framework.util.UserFeedBack.FeedbackKind;
import malitech.yadam.business.acquire.CopyJobStepRunner;
import malitech.yadam.business.acquire.JobStepRunner;

public class JobParameters extends BaseEntity
{

    private static final long       serialVersionUID        = 1L;

    private static final String     namePropertyName        = "Name";
    private static final String     descriptionPropertyName = "Description";
    private static final String     stepCountPropertyName   = "StepCount";

    private List<JobStepParameters> parameters              = new ArrayList<JobStepParameters>();

    private String                  name;
    private String                  description;
    

    public JobParameters()
    {

    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getDescription()
    {
        return description;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    public List<JobStepParameters> getParameters()
    {
        return parameters;
    }

    /**
     * Load the job with from the specified properties object
     * 
     * @param properties
     */
    public void load(String prefix, Properties properties)
    {
        DefaultFeedBackHandler.FeedBack.feedback(FeedbackKind.Info, "JobParameters.Load Started");

        String keyName = prefix + "." + namePropertyName;
        this.name = properties.getProperty(keyName);

        keyName = prefix + "." + descriptionPropertyName;
        this.description = properties.getProperty(keyName);

        keyName = prefix + "." + stepCountPropertyName;
        int stepCount = Integer.parseInt(properties.getProperty(keyName));
        for (int i = 0; i < stepCount; i++)
        {
            String s = String.valueOf(i);
            String childPrefix = prefix + "." + s;
            String clazzName = properties.getProperty(childPrefix+".ClazzName");
            JobStepParameters jobStep = createFromClazzName(clazzName);            

            jobStep.load(childPrefix, properties);
            parameters.add(jobStep);
            
        }
        DefaultFeedBackHandler.FeedBack.feedback(FeedbackKind.Info, "JobParameters.Load Ended");
        return;
    }

    /**
     * Save the job to the specified properties object all the entries names are formated <Prefix>.<PropertyName>
     * 
     * @param properties
     */
    public void save(String prefix, Properties properties)
    {
        DefaultFeedBackHandler.FeedBack.feedback(FeedbackKind.Info, "JobParameters.Save Started");
        String keyName = prefix + "." + namePropertyName;
        properties.put(keyName, this.name);
        keyName = prefix + "." + descriptionPropertyName;
        properties.put(keyName, this.description);

        keyName = prefix + "." + stepCountPropertyName;
        properties.put(keyName, String.valueOf(parameters.size()));

        // Save JobStepParameters
        int i = 0;
        for (JobStepParameters jobStepParameters : parameters)
        {
            String s = String.valueOf(i);
            String childPrefix = prefix + "." + s;
            jobStepParameters.save(childPrefix, properties);
        }
        DefaultFeedBackHandler.FeedBack.feedback(FeedbackKind.Info, "JobParameters.Save Ended");
        return;
    }
    
    private JobStepParameters createFromClazzName(String clazzName)
    {
        JobStepParameters returnValue = null;
        if ( "CopyJobStepParameters".equalsIgnoreCase(clazzName))
        {
            returnValue = new CopyJobStepParameters(this);
        }
        return returnValue;
    }
    
    public static JobStepRunner createRunnerFromClazzName(String clazzName)
    {
        JobStepRunner jobStepRunner = null;
        if ( "CopyJobStepParameters".equalsIgnoreCase(clazzName))
        {
            jobStepRunner = new CopyJobStepRunner();
        }
        return jobStepRunner;
    }
}
